package com.leyu.mall.admin.goods.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.common.facade.ICmsChannelFacade;
import com.leyu.mall.service.goods.IGoodsRelationService;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/relation")
public class GoodsRelationController {

	@Resource(name = "goodsRelationServiceImpl")
	private IGoodsRelationService goodsRelationServiceImpl;
	
	@Autowired
	private ICmsChannelFacade cmsChannelFacade;
	

//	@RequestMapping(value = "save", method = RequestMethod.POST)
//	public String save(Model model, GoodsRelation goodsRelation) {
//		try {
//			goodsRelationServiceImpl.save(goodsRelation);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return "redirect:../relation";
//	}

	@RequestMapping(value = "delete/{relationID}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long relationID) {
		try {
			goodsRelationServiceImpl.deleteGoodsRelation(relationID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../relation";
	}

	@RequestMapping(value = "edit/{relationID}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long relationID) {
		try {
			model.addAttribute("entity", goodsRelationServiceImpl
					.getGoodsRelationById(relationID));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editrelation.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request) {
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newRelation(Model model) throws Exception {
		System.out.println("---------------");
		
		model.addAttribute("channellist", cmsChannelFacade.getcmsChannelList());
		return "admin/newrelation.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		try {
		model.addAttribute("list", goodsRelationServiceImpl.getAllGoodsRelation());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/relation.html";
	}
}
